﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Reflection;
using System.ServiceModel;

namespace SBPweb.RIA.Client.Controls.Windows
{

    public partial class ErrorWindow : MessageWindow
    {
        
        #region Private fields

        private bool isShowStopper;

        #endregion

        #region Constructor logic

        public ErrorWindow(Exception e)
            : base()
        {
            InitializeComponent();
            SetUIContentBasedOnException(e);
        }

        public ErrorWindow(Canvas rootElement, Exception e)
            : base(rootElement)
        {
            InitializeComponent();
            SetUIContentBasedOnException(e);
        }

        public ErrorWindow(string rootElementName, Exception e)
            : base(rootElementName)
        {
            InitializeComponent();
            SetUIContentBasedOnException(e);
        }


        public ErrorWindow(Uri uri)
            : base()
        {
            InitializeFromUri(uri);
        }

        public ErrorWindow(Canvas rootElement, Uri uri)
            : base(rootElement)
        {
            InitializeFromUri(uri);
        }

        public ErrorWindow(string rootElementName, Uri uri)
            : base(rootElementName)
        {
            InitializeFromUri(uri);
        }


        public ErrorWindow(string message, string details)
            : base()
        {
            InitializeFromMessage(message, details);
        }

        public ErrorWindow(Canvas rootElement, string message, string details)
            : base(rootElement)
        {
            InitializeFromMessage(message, details);
        }

        public ErrorWindow(string rootElementName, string message, string details)
            : base(rootElementName)
        {
            InitializeFromMessage(message, details);
        }


        private void InitializeFromMessage(string message, string details)
        {
            InitializeComponent();
            ErrorTextBox.Text = message + Environment.NewLine + Environment.NewLine + details;
        }

        private void InitializeFromUri(Uri uri)
        {
            InitializeComponent();
            if (uri != null)
            {
                ErrorTextBox.Text = "Page not found: \"" + uri.ToString() + "\"";
            }
        }

        #endregion

        #region Property accessors

        public bool IsShowStopper
        {
            get
            {
                return isShowStopper;
            }
            set
            {
                if (isShowStopper = value)
                {
                    OKButton.Visibility = Visibility.Collapsed;
                    this.HasCloseButton = false;
                }
            }
        }

        #endregion

        #region Methods

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        protected override void InitializeDefaultValues()
        {
            base.InitializeDefaultValues();
            IsPersistable = false;
        }

        public void SetUIContentBasedOnException(Exception e)
        {
            if (e is TargetInvocationException)
            {
                e = (e as TargetInvocationException).InnerException;
            }
            FaultException<string> f = e as FaultException<string>;
            if (f != null)
            {
                ErrorTextBox.Text = f.Message + Environment.NewLine + f.Detail + Environment.NewLine + Environment.NewLine + f.StackTrace;
                tbrType.Text = f.Code.Name;
            }
            else
                if (e != null)
                {
                    ErrorTextBox.Text = e.Message + Environment.NewLine + Environment.NewLine + e.StackTrace;
                    tbrType.Text = e.GetType().Name;
                }
            if (e is UnauthorizedAccessException)
            {
                IsShowStopper = true;
            }
        }

        #endregion

    }

}